package zhizhemodi.tech.short_term_bwork.service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import zhizhemodi.tech.short_term_bwork.entity.PlanAndProgram;
import zhizhemodi.tech.short_term_bwork.entity.Program;
import zhizhemodi.tech.short_term_bwork.mapper.ProgramMapper;

@Repository
public class ProgramService {
    
    @Autowired
    ProgramMapper programMapper;

    @Resource
    ProgramFileListService programFileListService;

    @Resource
    PlanAndProgramService planAndProgramService;

    /**
     * 通过节目ID获取节目
     * @param programId
     * @return
     */
    public Program getProgramById(Integer programId) {
        QueryWrapper<Program> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("program_id", programId);
        return programMapper.selectOne(queryWrapper);
    }

    public List<Program> findProgram(String programName) {
        QueryWrapper<Program> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("program_name", programName);
        return programMapper.selectList(queryWrapper);
    }

    /**
     * 获取节目列表
     * @param dId
     * @param rowNum
     * @param pageNum
     * @return
     */
    public List<Program> getProgramList(Integer dId, Integer rowNum, Integer pageNum) {
        QueryWrapper<Program> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id", dId);
        queryWrapper.last(" limit " + pageNum * rowNum + "," + rowNum);
        return programMapper.selectList(queryWrapper);
    }

    /**
     * 添加节目
     * @param program
     */
    public void addProgram(Program program) {
        List<Program> programs = programMapper.selectList(null);
        Integer id = 0;
        if(programs.size() != 0) id = programs.get(programs.size() - 1).getProgramId() + 1;
        if(findProgram(program.getProgramName()).size() == 0) {
            program.setProgramId(id);
            programMapper.insert(program);
        }
    }

    /**
     * 删除节目
     * @param programId
     */
    public void deleteProgram(Integer programId) {
        programFileListService.deleteByProgramId(programId);
        planAndProgramService.deleteByProgramId(programId);
        QueryWrapper<Program> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("program_id", programId);
        programMapper.delete(queryWrapper);
    }

    public List<Map<String, Object>> getMsgByPlanId(Integer planId) {
        List<Map<String, Object>> re = new LinkedList<>();
        List<PlanAndProgram> programs = planAndProgramService.getPlanAndProgramByPlanId(planId);
        for(PlanAndProgram planAndProgram : programs) {
            Map<String, Object> singlePro = new HashMap<>();
            singlePro.put("total_time", getProgramById(planAndProgram.getProgramId()).getTotalTime());
            singlePro.put("each_time", 5);
            singlePro.put("img", programFileListService.getFileListByProgramId(planAndProgram.getProgramId()));
            re.add(singlePro);
        }
        return re;
    }
}
